﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.WebApi
{
    public partial class Config
    {
        private static object s_lockObj = new object();
        private static Config s_current;
        public static Config Current
        {
            get
            {
                if(s_current == null)
                {
                    lock(s_lockObj)
                    {
                        if(s_current == null)
                        {
                            string configFile = System.Configuration.ConfigurationManager.AppSettings["WebApi2.Config"];
                            if(string.IsNullOrEmpty(configFile))
                            {
                                configFile = System.Web.HttpContext.Current.Server.MapPath("~/Configs/WebApi2.config");
                            }
                            else
                            {
                                configFile = System.Web.HttpContext.Current.Server.MapPath(configFile);
                            }
                            s_current = ConfigDocument.Load(configFile);
                        }
                    }
                }
                return s_current;
            }
        }

        public Config()
        {
            Modules = new List<Module>();
        }

        /// <summary>
        /// 认证模式
        /// </summary>
        public string AuthMode { get; set; }

        /// <summary>
        /// 认证提供者
        /// </summary>
        public IAuthProvider AuthProvider { get; set; }

        /// <summary>
        /// 首字母是否小写,是否采用驼峰式命名法
        /// </summary>
        public bool IsCamelCasePropertyName { get; set; }

        public List<Module> Modules { get;  private set; }

        public Module GetModule(string name)
        {
            for (int index = 0; index < Modules.Count; index++)
            {
                if (string.Equals(Modules[index].Name, name))
                {
                    return Modules[index];
                }
            }
            return null;
        }

        public void UpdateModule(Module module)
        {
            for (int index  = 0; index  < Modules.Count; index ++)
            {
                if(string.Equals(Modules[index].Name, module.Name))
                {
                    Modules[index] = module;
                    break;
                }
            }
        }

        public Config Clone()
        {
            Config config = new Config();
            config.AuthMode = this.AuthMode;
            config.AuthProvider = this.AuthProvider;
            config.IsCamelCasePropertyName = this.IsCamelCasePropertyName;
            foreach (Config.Module srcModule in this.Modules)
            {
                Config.Module module = new Module();
                module.Name = srcModule.Name;
                module.Type = srcModule.Type;
                module.Assembly = srcModule.Assembly;

                config.Modules.Add(module);

                //foreach (Config.Action srcAction in module.Actions)
                //{
                //    Config.Action action = new Action();
                //    action.Name = srcAction.Name;
                //    action.Scenario = srcAction.Scenario;
                //    action.Type = srcAction.Type;

                //    module.Actions.Add(action);
                //}
            }

            return config;
        }
    }
}
